import RegisterApi from "../../api/register";

Page({
    data: {
        phone:'',
        code:'',
        codeFlag:false,
        time: 60 * 1000
    },
    onLoad: function (options) {
        //把组织机构置为空
        wx.setStorageSync('organizeCode','')
        wx.setStorageSync('organizeName','')
        debugger
        const phone = options.phone || ''
        this.setData({
            phone:phone
        })
    },
    /**
     * 返回
     */
    handlerGobackClick(){
        wx.navigateBack()
    },
    /**
     * 下一步
     */
    async nextStep(){
        const phone = this.data.phone
        const code = this.data.code
        if(phone == ''){
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if(code == ''){
            wx.showToast({
                title: '请输入验证码',
                icon: 'none',
                duration: 2000
            })
            return
        }
        //判断验证码是否正确
        const param = {
            telNum :phone,
            verifyCode:code
        }
        const res = await RegisterApi.checkVerifyCode(param)
        if(res.code == 100){
            wx.showToast({
                title:res.message,
                icon: 'none',
                duration: 2000
            })
        }else{
            wx.navigateTo({
                url: `/pages/register2/register2?phone=${phone}`
            })
        }
    },
    /**
     * 发送验证码
     */
    async sendCode(){
        if (this.data.codeFlag){
            return
        }
        const phone = this.data.phone
        if(phone == ''){
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 2000
            })
            return
        }
        //验证手机号是否正确
        if(!(/^1[3456789]\d{9}$/.test(phone))){
            wx.showToast({
                title: '请输入正确手机号',
                icon: 'none',
                duration: 2000
            })
            return
        }
        //查询用户是否存在
        const param = {
            telNum:phone
        }
        let res = await  RegisterApi.sendCode(param)
        if(res.code == 100){
            wx.showToast({
                title: res.message,
                icon: 'none',
                duration: 2000
            })
        }else{
            //如果存在，开始倒计时
            this.setData({
                codeFlag:true
            })
            this.start()
        }

    },
    /**
     * input 数据绑定
     */
    updateValue(e) {
        let name = e.currentTarget.dataset.name;
        let nameMap = {}
        nameMap[name] = e.detail
        this.setData(nameMap)
    },
    start() {
        const countDown = this.selectComponent('.control-count-down');
        countDown.start();
    },
    finished() {
        this.setData({
            codeFlag:false
        })
    },
});
